#include "friend.h"

Friend::Friend(QWidget *parent) : QWidget(parent)
{
    m_pShowMsgTE = new QTextEdit;
    m_pFriendListWidget = new QListWidget;
    m_pInputMsgLE = new QLineEdit;
    m_pDelFriendPB = new QPushButton("删除");
    m_pFlushFriendPB = new QPushButton("刷新") ;
    m_pShowOnlineUsrPB = new QPushButton("显示在线用户");
    m_pSearchUsrPB = new QPushButton("搜索用户");
    m_pMsgSendPB = new QPushButton("群聊");
    m_pPrivateChatPB = new QPushButton("私聊");
    //垂直布局
    QVBoxLayout* pRightPBVBL=new QVBoxLayout;
    pRightPBVBL->addWidget(m_pDelFriendPB);
    pRightPBVBL->addWidget(m_pFlushFriendPB);
    pRightPBVBL->addWidget(m_pShowOnlineUsrPB);
    pRightPBVBL->addWidget(m_pSearchUsrPB);
    pRightPBVBL->addWidget(m_pPrivateChatPB);
    //水平布局
    QHBoxLayout* pTopHBL = new QHBoxLayout;
    pTopHBL->addWidget(m_pShowMsgTE);
    pTopHBL->addWidget(m_pFriendListWidget);
    pTopHBL->addLayout(pRightPBVBL);

    QHBoxLayout* pMsgHBL = new QHBoxLayout;
    pMsgHBL->addWidget(m_pInputMsgLE);
    pMsgHBL->addWidget(m_pMsgSendPB);

    m_pOnline = new Online;

    QVBoxLayout* pMain = new QVBoxLayout;
    pMain->addLayout(pTopHBL);
    pMain->addLayout(pMsgHBL);
    pMain->addWidget(m_pOnline);
    m_pOnline->hide();
    connect(m_pShowOnlineUsrPB,SIGNAL(clicked(bool)),this,SLOT(ShowOnlineUsrPB_clicked()));
    connect(m_pSearchUsrPB,SIGNAL(clicked(bool)),this,SLOT(SearchUsrPB_clicked()));


    setLayout(pMain);
}

void Friend::ShowAllUser(PDU *pdu)
{
   if(pdu==NULL)
       return;
   m_pOnline->showUser(pdu);
}



void Friend::ShowOnlineUsrPB_clicked()
{
    if(m_pOnline->isHidden())
    {
        m_pOnline->show();
        //获取在线用户，并显示
        //1、发送请求
        PDU* pdu=mkPDU(0);
        pdu->uiMsgType=ENUM_MSG_TYPE_ALL_ONLINE_USER_REQUEST;
        TcpClient::getinstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
        //2、收到数据并显示，显示在online界面上的用户列表上


    }
    else
    {
        m_pOnline->hide();
    }
}

void Friend::SearchUsrPB_clicked()
{
    //1、发送请求
    PDU* pdu=mkPDU(0);
    pdu->uiMsgType = ENUM_MSG_TYPE_SEARCH_USER_REQUEST;
    //使用对话框，输入用户名
    QString usrName = QInputDialog::getText(this,"搜索","用户名:");
    memcpy(pdu->odata,usrName.toStdString().c_str(),usrName.size());
    TcpClient::getinstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
    //将pdu释放
    free(pdu);
    pdu=NULL;
}



















